package vn.vfriends.soffice.facade;

import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import vn.vfriends.soffice.entity.Dept;

/**
 *
 * @author chaungoctuan@gmail.com
 */
@Stateless
@LocalBean
public class DeptFacade extends AbstractFacade<Dept> {

    @PersistenceContext(unitName = "sOfficePU")
    private EntityManager em;

    public DeptFacade() {
        super(Dept.class);
    }
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    public Dept findByCode(String code){
        Query query = em.createNamedQuery("Dept.findByCode", Dept.class);
        query.setParameter("code", code);
        List<Dept> deptList = query.getResultList();
        if(deptList != null && !deptList.isEmpty())
            return deptList.get(0);
        else
            return null;
    }
    
    public Dept findByName(String name){
        Query query = em.createNamedQuery("Dept.findByName", Dept.class);
        query.setParameter("name", name);
        List<Dept> deptList = query.getResultList();
        if(deptList != null && !deptList.isEmpty())
            return deptList.get(0);
        else
            return null;
    }
}
